package com.geeklan.smart.ui.pet;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.Toast;

import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.geeklan.base.base.BaseActivity;
import com.geeklan.smart.R;
import com.geeklan.smart.data.model.PetInfo;
import com.geeklan.smart.data.repository.pet.PetRepository;
import com.geeklan.smart.databinding.ActivityMyPetBinding;
import com.geeklan.smart.ui.pet.adapter.PetListAdapter;
import com.geeklan.smart.ui.viewmodel.pet.PetViewModel;
import com.geeklan.smart.ui.viewmodel.pet.PetViewModelFactory;
import com.geeklan.smart.widget.ItemOnClickListener;

import java.util.ArrayList;
import java.util.List;

public class MyPetActivity extends BaseActivity<ActivityMyPetBinding> implements View.OnClickListener , ItemOnClickListener {
    private PetViewModel petViewModel;
    private List<PetInfo> petInfos = new ArrayList<>();
    private PetListAdapter mAdapter;
    @Override
    protected ActivityMyPetBinding initBinding() {
        return ActivityMyPetBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        petViewModel = new ViewModelProvider(this, new PetViewModelFactory(new PetRepository())).get(PetViewModel.class);

        petViewModel.getPetListLiveData().observe(this, response -> {
            if(response != null && !response.isEmpty()){
                petInfos.clear();
                petInfos.addAll(response);
                mAdapter.notifyDataSetChanged();
            }

            if(petInfos.isEmpty()){
                mBinding.llNotData.setVisibility(View.VISIBLE);
                mBinding.rvPet.setVisibility(View.GONE);
            }else {
                mBinding.llNotData.setVisibility(View.GONE);
                mBinding.rvPet.setVisibility(View.VISIBLE);
            }
            mBinding.swipeRefreshLayout.setRefreshing(false);
        });

        petViewModel.getErrorLiveData().observe(this, s -> {
            Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
            mBinding.swipeRefreshLayout.setRefreshing(false);
        });
//        petViewModel.getPetList();

    }

    @Override
    protected void onResume() {
        super.onResume();
        mBinding.swipeRefreshLayout.setRefreshing(true);
        petViewModel.getPetList();
    }

    @Override
    protected void initView() {
        mBinding.ivBack.setOnClickListener(v -> finish());
        mBinding.ivRight.setOnClickListener(this);

        mAdapter = new PetListAdapter(this,petInfos);
        mAdapter.setItemOnClickListener(this);
        mBinding.rvPet.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rvPet.setAdapter(mAdapter);


        mBinding.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                petViewModel.getPetList();
            }
        });
//        mBinding.swipeRefreshLayout.setRefreshing(true);
    }

    @Override
    protected void onApplyWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            getWindow().setStatusBarColor(Color.WHITE);
            return insets;
        });
    }

    @Override
    public void networkStatus(boolean isConnected) {

    }

    @Override
    public void networkType(int type) {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == mBinding.ivRight.getId()){
            AddPetActivity.startActivity(this);
        }
    }
    public static void startActivity(Context context){
        Intent intent = new Intent(context, MyPetActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void OnItemClick(View view, int position) {
        PetInfo petInfo = petInfos.get(position);
        EditPetActivity.startActivity(this,petInfo);
    }
}